<%#
 Copyright 2013-2018 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
# JHipster Registry HA cluster
#
# Note that as this is based on a StatefulSet, it will only work on Kubernetes >= 1.5
#
# By default, the JHipster Registry and its UI is not accessible from outside the cluster for security reasons
# You can setup temporary access to it on localhost:8761 by running :
#   kubectl port-forward jhipster-registry-0 8761
#
# To scale your JHipster Registry cluster :
#   In this file, change the value of spec.replicas and CLUSTER_SIZE to any value
#   Apply the descriptor again : `kubectl apply -f jhipster-registry.yml`
#   This will create new replicas with the correct CLUSTER_SIZE which is mandatory so that all Eureka server can connect directly to all the others.
#   Then delete the previous replica pods one by one so that they can be recreated with the correct CLUSTER_SIZE configuration.
#   `kubectl delete pod jhipster-registry-0`, `kubectl delete pod jhipster-registry-1`
apiVersion: v1
kind: Secret
metadata:
  name: registry-secret
  namespace: <%= kubernetesNamespace %>
type: Opaque
data:
  registry-admin-password: <%= adminPasswordBase64 %> # base64 encoded "<%= adminPassword %>"
---
apiVersion: v1
kind: Service
metadata:
  name: jhipster-registry
  namespace: <%= kubernetesNamespace %>
  labels:
    app: jhipster-registry
  annotations:
    service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
spec:
  ports:
  - port: 8761
    name: http
  clusterIP: None
  selector:
    app: jhipster-registry
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: jhipster-registry
  namespace: <%= kubernetesNamespace %>
spec:
  serviceName: jhipster-registry
  replicas: 2
  selector:
    matchLabels:
      app: jhipster-registry
      version: "1.0"
  template:
    metadata:
      labels:
        app: jhipster-registry
        version: "1.0"
     <%_ if (istio !== 'no') { _%>
      annotations:
        sidecar.istio.io/inject: "false"
     <%_ } _%>
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - name: jhipster-registry
        image: <%= DOCKER_JHIPSTER_REGISTRY %>
        ports:
        - containerPort: 8761
        env:
        # StatefulSet specific configuration
        # Registry configuration
        - name: SPRING_PROFILES_ACTIVE
          value: prod,k8s
        - name: SPRING_SECURITY_USER_PASSWORD
          valueFrom:
            secretKeyRef:
              name: registry-secret
              key: registry-admin-password
        <%_ if (istio !== 'no') { _%>
        - name: EUREKA_INSTANCE_PREFER_IP_ADDRESS
          value: "false"
        <%_ } _%>
        - name: JHIPSTER_SECURITY_AUTHENTICATION_JWT_SECRET
          value: bXktc2VjcmV0LXRva2VuLXRvLWNoYW5nZS1pbi1wcm9kdWN0aW9uLWFuZC10by1rZWVwLWluLWEtc2VjdXJlLXBsYWNl
        - name: SPRING_CLOUD_CONFIG_SERVER_COMPOSITE_0_TYPE
          value: native
        - name: SPRING_CLOUD_CONFIG_SERVER_COMPOSITE_0_SEARCH_LOCATIONS
          value: file:./central-config
        - name: EUREKA_INSTANCE_LEASE_RENEWAL_INTERVAL_IN_SECONDS
          value: '15'
        - name: EUREKA_INSTANCE_LEASE_EXPIRATION_DURATION_IN_SECONDS
          value: '30'
        - name: EUREKA_SERVER_PEER_EUREKA_NODES_UPDATE_INTERVAL_MS
          value: '15000'
        - name: EUREKA_SERVER_RENAWAL_THRESHOLD_UPDATE_INTERVAL_MS
          value: '15000'
        - name: EUREKA_SERVER_REGISTRY_SYNC_RETRIES
          value: '3'
        - name: EUREKA_SERVER_ENABLE_SELF_PRESERVATION
          value: 'false'
        - name: EUREKA_SERVER_PEER_NODE_CONNECT_TIMEOUT_MS
          value: '2000'
        - name: EUREKA_CLIENT_FETCH_REGISTRY
          value: 'true'
        - name: EUREKA_CLIENT_REGISTER_WITH_EUREKA
          value: 'true'
        - name: K8S_CONFIG_PATH
          value: '/central-config/'
        volumeMounts:
        - name: config-volume
          mountPath: /central-config
      volumes:
      - name: config-volume
        configMap:
          name: application-config
